﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using EnglishRegister.Business;
using EnglishRegister.Entities;

namespace EnglishRegister.Forms
{
    public partial class frmStudent : Form
    {
        private Dictionary<Guid, string> comboValues;

        private StudentModel selectedStudent;

        public frmStudent()
        {
            InitializeComponent();
        }

        private void ClearValues()
        {
            this.txtAddress.Clear();
            this.txtFirstName.Clear();
            this.txtLastName.Clear();
            this.txtPhone.Clear();
        }

        private void LoadGridview()
        {
            Guid CourseId = ((KeyValuePair<Guid, string>)this.cmbCourse.SelectedItem).Key;
            IList data = StudentBL.GetAllStudentByCourse(CourseId);
            this.grvStudents.DataSource = data;
            this.grvStudents.Columns["ID"].Visible = false;
        }

        private void LoadGridview(IList data)
        {
            this.grvStudents.DataSource = data;
        }

        private void LoadCombobox()
        {
            comboValues = CourseBL.GetDictionaryCourseName();

            BindingSource source = new BindingSource(comboValues, null);

            this.cmbCourse.DataSource = source;
            this.cmbCourse.DisplayMember = "Value";
            this.cmbCourse.ValueMember = "Key";

            this.cmbRegisterCourse.DataSource = source;
            this.cmbRegisterCourse.DisplayMember = "Value";
            this.cmbRegisterCourse.ValueMember = "Key";
        }

        private void StudentManagerForm_Load(object sender, EventArgs e)
        {
            this.LoadCombobox();
        }

        private void cmbCourse_Click(object sender, EventArgs e)
        {
            this.LoadCombobox();
        }

        private void cmbCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            KeyValuePair<Guid, string> selectedValue = (KeyValuePair<Guid, string>)this.cmbCourse.SelectedItem;
            IList data = StudentBL.GetAllStudentByCourse(selectedValue.Key);
            this.LoadGridview(data);
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            this.tbcMain.SelectedTab = this.tbpRegister;
            this.cmbRegisterCourse.SelectedIndex = this.cmbCourse.SelectedIndex;
        }

        private bool CheckValid()
        {
            Guid courseID = this.GetGuidFromCombo(this.cmbRegisterCourse);
            int count = StudentBL.GetAllStudentByCourse(courseID).Count;
            if (count >= CourseBL.GetByPrimaryKey(courseID).LimitPeople.Value)
            {
                Utilities.UtilitiesTools.ShowErrorMessage("Class is full!");
                return false;
            }
            return true;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckValid())
            {
                if (selectedStudent == null)
                {
                    selectedStudent = new StudentModel();
                    this.GetInfoOnForm(selectedStudent);
                    StudentBL.Insert(selectedStudent);
                }
                else
                {
                    this.GetInfoOnForm(selectedStudent);
                    StudentBL.Update(selectedStudent);

                }
                this.selectedStudent = null;
                this.LoadGridview();
                this.tbcMain.SelectedIndex = 0;
            }

        }

        private Guid GetGuidFromCombo(ComboBox cmb)
        {
            return ((KeyValuePair<Guid, string>)cmb.SelectedItem).Key;
        }

        private void GetInfoOnForm(StudentModel obj)
        {
            obj.FirstName = this.txtFirstName.Text;
            obj.LastName = this.txtLastName.Text;
            obj.Address = this.txtAddress.Text;
            obj.PhoneNo = this.txtPhone.Text;
            obj.CourseID = GetGuidFromCombo(this.cmbRegisterCourse);
        }

        private void grvStudents_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex != 0) return;

            int ID = (int)this.grvStudents.Rows[e.RowIndex].Cells["ID"].Value;
            if (DialogResult.Yes == Utilities.UtilitiesTools.ShowWarning("Continue deleting?"))
            {
                StudentBL.Delete(ID);
                this.LoadGridview();
            }
        }

        private void tbcMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tbcMain.SelectedIndex == 0) this.selectedStudent = null;
            if (this.tbcMain.SelectedTab == this.tbpRegister)
            {
                if (selectedStudent == null)
                {
                    this.ClearValues();
                }
                else
                {
                    this.txtAddress.Text = selectedStudent.Address;
                    this.txtFirstName.Text = selectedStudent.FirstName;
                    this.txtLastName.Text = selectedStudent.LastName;
                    this.txtPhone.Text = selectedStudent.PhoneNo;
                }
            }
        }

        private void grvStudents_DoubleClick(object sender, EventArgs e)
        {
            if (this.grvStudents.SelectedRows == null || this.grvStudents.SelectedRows.Count == 0) return;
            int ID = (int)this.grvStudents.SelectedRows[0].Cells["ID"].Value;
            this.selectedStudent = StudentBL.GetByPrimaryKey(ID);
            this.tbcMain.SelectedTab = this.tbpRegister;

        }
    }
}
